<?php
App::import('Lib', 'Facebook.FB');
App::import('Controller', 'TimeLines');
class UsersController extends AppController
{
   
    public function index(){
        $this->set('title_for_layout', "My TimeLife");
        /*$FacebookApi = new FB();
        $FacebookApi->api('/me');
        $friendList = $FacebookApi->api('/me/friendlists');
        $this->set('teste', $FacebookApi->api('/'.$friendList['data'][2]['id'].'/members
         */           
        $this->layout = 'home';
        $timelineId = $this->User->query('SELECT * FROM time_lines WHERE user_id = '.$this->Auth->user('id').' ORDER BY create_date DESC');
        
        /* Se o usuario não possui um timeline então cria-se */
        if(count($timelineId) == 0)
        {
            $TimeLines = new TimeLinesController;
            $TimeLines->constructClasses();
            $TimeLines->create();
            $timelineId = $TimeLines->last();
        }
        
        $this->set('time_lines', $timelineId);
        $this->set('momentid', $this->User->query('SELECT id FROM moments WHERE time_line_id = '.$timelineId[0][0]['id'].' ORDER BY create_hour DESC LIMIT 1'));
    }
    
    public function login(){
        $this->set('title_for_layout', "Login");
        $this->layout = 'login';
        if($this->Connect->user())
            $this->redirect(array('controller' => 'users'));
    }
    
    public function logout()
    {
        $this->Auth->logout();
        $this->redirect('/');
    }
    
    public function myaccount()
    {
        $this->set('title_for_layout', "My Account");
        /*$this->set('user', $this->User->findById($this->Auth->user('id')));*/
        $this->layout = 'home';
        $this->set('teste', $this->User->find('all'));
    }
    
    public function saveEdit()
    {
        $this->loadModel('Message');
        $data = array(
          "name"  => $this->data['Myaccount']['name'],
          "about" => $this->data['Myaccount']['about'],
        );
        $this->User->set($data);
        $this->User->save();
        $this->Session->write('Auth.User.name', $data['name']);
        $this->Session->write('Auth.User.about', $data['about']);
        $message = array(
                  'message' => 'Account successfully edited',
                  'user_id' => $this->Auth->User('id'),
                  'status' => 1,
            );
        $this->Message->save($message);
        $this->redirect("myaccount/");
    }
    
    public function timeline($id)
    {
         if ($this->RequestHandler->isAjax()) { 
              $this->set('id', $id);
         $this->render('/elements/timelineview/'); 
        } 
    }
    
    public function moment($id)
    {
         if ($this->RequestHandler->isAjax()) { 
// Render the element from the action 
              $this->set('momentid', $id);
         $this->render('/elements/momentview/'); 
        } 
    }
    
    public function friend()
    {
        $this->layout = 'home';
        $this->set('title_for_layout', "My Friends");
        $FacebookApi = new FB();
        $FacebookApi->api('/me');
        $friendList = $FacebookApi->api('/me/friends');
        $countId = count($friendList['data']);
        $string="";
        for($i=0;$i<$countId;$i++)
        {
            $resp = $this->User->query('SELECT name,about FROM users WHERE facebook_id = '.$friendList['data'][$i]['id'].'');
            if($resp)
            {
                $string=$string.'<div class="span-2"><img src="https://graph.facebook.com/'.$friendList['data'][$i]['id'].'/picture"/></div>'
                        .'<div class="span-15"><h2>'.$friendList['data'][$i]['name'].'</h2><p><h3>About</h3>'.' '.$resp[0][0]['about'].'</p></div>'
                        .'<div class="span-2"><br /><h4>TimeLife</h4></div><hr />';
                $resp=0;
            }
        }
        $this->set('string',$string);        
    }
    
}
?>